package sort;

import java.util.Arrays;

/**
 * 希尔排序
 *
 * 大事化小小事化了
 *
 * 如果一个数比它后面arr.length/2位数大 就交换
 第1 轮
 [32, 223, 54, 23, 6, 123, 315, 342, 532, 861]
 第2 轮
 [6, 23, 32, 123, 54, 223, 315, 342, 532, 861]
 第3 轮
 [6, 23, 32, 54, 123, 223, 315, 342, 532, 861]
 *
 *
 * @author shkstart
 * @create 2021-04-30 15:56
 */
public class ShellSort {
    public static void main(String[] args) {
        int[] arr = {32, 315, 54, 23, 861, 123,223,342,532,6};
        shellSort(arr);
//        System.out.println(Arrays.toString(arr));
    }

    private static void shellSort(int[] arr) {
        int count = 0;
        int num = 0;
        for (int grp = arr.length/2; grp >0; grp/=2){
            for (int i = grp; i < arr.length; i++) {
                for (int j = i - grp; j >= 0; j -= grp) {
                    if (arr[j] > arr[j + grp]) {
                        num = arr[j];
                        arr[j] = arr[j+grp];
                        arr[j+grp] = num;
                    }
                }
            }
            System.out.println("第"+ (++count)+" 轮");
            System.out.println(Arrays.toString(arr));
        }

    }
}
